package DataBase;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import DataObjects.Item;

public class AddNewInventoryItem {
	private Item item;
	private Integer flagExists=0;
	public AddNewInventoryItem(Item item){
		this.item = item;
	}
	
	public int checkExistItem()
	{
		SQLConnection sc=new SQLConnection();
		try {
			PreparedStatement ps = sc.getConnection().prepareStatement("Select iid from item where iname=?;");
			ps.setString(1,(String)this.item.getIname());
			ResultSet rs=ps.executeQuery();	
			 while(rs.next())
		        {   
				 flagExists=1;
		        }
			
		}
		catch(SQLException e){
			e.printStackTrace();
			flagExists=1;
			
		}
		return flagExists;
	}
	
	
	
	
	public boolean saveItem(){
		SQLConnection sc=new SQLConnection();
		try {
			PreparedStatement ps = sc.getConnection().prepareStatement("INSERT INTO item(iid, iname, icategory, iexpiration, imin_quantity, imax_quantity, iunit_of_measure,IStoreQuantity,Icost) VALUES(?,?,?,?,?,?,?,'0',?);");
			//sc.getConnection().setAutoCommit(false);
			ps.setString(1,(String)this.item.getIid());
			ps.setString(2,(String)this.item.getIname());
			ps.setString(3,(String)this.item.getICategory());
			ps.setString(4,(String)this.item.getIExpiration());
			ps.setString(5,(String)this.item.getIMin_quantity());
			ps.setString(6,(String)this.item.getIMax_quantity());
			ps.setString(7,(String)this.item.getIUnit_of_measure());
			ps.setString(8,(String)this.item.getIcost());
			ps.executeUpdate();
			//sc.getConnection().commit();
			sc.getConnection().close();
		}
		catch(SQLException e){
			e.printStackTrace();
			
			return false;
		}
		return true;
	}
}
